//
//  VWPlayerSecondControlLayer.swift
//  VideoWatcher
//
//  Created by JY_NEW on 2021/10/18.
//  Copyright © 2021 JunYue. All rights reserved.
//

import UIKit
import ijkPlayerManager

/// 二级控制层基类，不包含contentView
class VWPlayerSecondBaseControlLayer: VWPlayerBaseLayer {
    
    required override init() {
        super.init()
    }
    
    override var category: VideoPlayerLayerCategory {
        return .control
    }
    ///执行具体的操作后将自己关闭并不再显示回基础功能
    func doAction(_ action: () -> Void) {
        self.manager?.layers.controls.deactive()
        action()
    }
}
/// 二级控制层基类
class VWPlayerSecondControlLayer: VWPlayerSecondBaseControlLayer {
    /// 只显示在全屏下
    var isFullScreenOnly: Bool {
        return false
    }
    override func loadPlayerManager(_ player: VideoPlayerManagerType) {
        player.observer.fullScreen.addTarget( self, action: #selector(self.onFullScreenChanged))
    }
    override func unloadPlayerManager(_ player: VideoPlayerManagerType) {
        player.observer.fullScreen.removeTarget(self)
    }
    override func loadContainerView(_ containerView: UIView) {
        let isFullScreen = self.playerManager?.isFullScreen ?? false
        self.contentView.applyLayout(.width(isFullScreen ? 300 : 200))
        containerView.add(self.contentView, layout: .marginY, .right)
    }
    
    @objc func onFullScreenChanged() {
        let isFullScreen = self.playerManager?.isFullScreen ?? false
        if self.isFullScreenOnly && !isFullScreen {
            self.deactive()
        }else{
            let width: CGFloat = isFullScreen ? 300 : 200
            self.contentView.applyLayout(.update(.width(width)))
        }
    }
    
    lazy var contentView: UIView = UIView(.bg(.init(white: 0, alpha: 0.65)))
    
    override func show(completion:@escaping () -> Void) {
        self.aniamteShow(for: self.contentView, from: .right, completion: completion)
    }
    override func hide(completion:@escaping () -> Void) {
        self.aniamteHide(for: self.contentView, to: .right, completion: completion)
    }
}
